import React, { Component } from "react";
import { Switch, Route, withRouter, Redirect } from "react-router-dom";
import routers from "../router/index";
class RouterView extends Component {
  util = (routerList) => {
    return routerList.map((route) => {
      if (route.children) {
        return (
          <Route
            key={route.name}
            path={route.path}
            render={() => (
              <route.component key={route.name}>
                {this.util(route.children)}
                <Route
                  key={route.name}
                  path={route.path}
                  exact
                  render={() => <Redirect to={route.redirect} />}
                />
              </route.component>
            )}
          />
        );
      } else {
        return (
          <Route
            key={route.name}
            path={route.path}
            exact
            render={() => {
              return route.redirect ? (
                <Redirect to={route.redirect} />
              ) : (
                <route.component />
              );
            }}
          />
        );
      }
    });
  };
  render() {
    return <Switch>{this.util(routers)}</Switch>;
  }
}
export default withRouter(RouterView);
